package com.guc.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 演示session管理机制
 */
@WebServlet(urlPatterns = "/s")
public class SessionServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从request获取session对象
        HttpSession session = req.getSession();
        //获取session 0ID
        String id = session.getId();
        System.out.println(id);
        //观察id是否是同一个session
        req.setAttribute("id",session.getId());
        req.getRequestDispatcher("info.jsp").forward(req,resp);
       session.invalidate();//session失效
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
